/**
 * @description 替换传入链接 协议域名端口 为当前项目地址协议域名端口
 * @param {String} url 链接
 * @param {Object} config 配置项
 * @param {Boolean} config.changePort 是否替换端口
 * @param {URL} config.targetDomin 替换目标地址
 * @param {Boolean} config.changeProtocol 是否替换协议
 * @returns {String} 替换后的链接
 */
const urlDomainReplace = function (url:any, config = {}) {
	if (!url) {
		return '';
	}
	const currentConfig = {
		...{
			changePort: true,
			changeProtocol: true,
			targetDomin: window.location,
		},
		...config,
	};
	const old = new URL(url);
	const protocol = currentConfig.changeProtocol ? currentConfig.targetDomin.protocol : old.protocol;
	let port = '';
	if (!currentConfig.changePort) {
		port = old.port ? ':' + old.port : '';
	} else {
		port = currentConfig.targetDomin.port ? ':' + currentConfig.targetDomin.port : '';
	}
	const newUrl =
		protocol + '//' + currentConfig.targetDomin.hostname + port + old.pathname + old.search;
	return newUrl;
};
export default urlDomainReplace;
